package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.common.utils.DateUtils;
import a66weding.com.jiehuntong.common.utils.FileUtil;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.RegularUtil;
import a66weding.com.jiehuntong.common.utils.StrUtil;
import a66weding.com.jiehuntong.common.utils.stubar.StatusBarUtil;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/8/19.
 * 说明: 任务详情页面
 */
public class JobDetailActivity extends Activity implements View.OnClickListener{
    //任务名称
    private TextView  job_detail_photoname;
    private  MarriageProposalBean.DataBean dataBean;
    //是否完成;
    private TextView job_detail_isdone;
    //编辑
    private TextView task_editor_tv;
    //预计消费金额
    private TextView job_detail_Consumption_expected_money_tv;
    //任务指派人员
    private TextView job_detail_missionPersonnel_name_tv;
    //店铺
    private TextView job_detail_book_place;
    //备注
    private TextView job_detail_remark_content_tv;
    //头像
    private CircularImage job_detail_photo;
    //头像的名字
    private String HEADName;
    private Bitmap headBitmap;
    private int flag ;
    private int days;
    private  TextView tian_shu_tv;
    //图片地址
    private String picurl;
    //剩余天数
    private TextView tian;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_details1);
        StatusBarUtil.setColor(this,getResources().getColor(R.color.job_detail_bg_color),1);
        dataBean = (MarriageProposalBean.DataBean) getIntent().getSerializableExtra(JobDetail.TASK_FLAG);
        flag = getIntent().getIntExtra("flag",0);
        //图片地址;
        picurl = getIntent().getStringExtra("headpictrue");
        /**DataBean
         * {WeddingBookID=391,
         * BookTitle='登记', BookDate='',
         * RemindDate='', BookPlace='',
         * IsComplete=false, RelationName='',
         * PayMoney=0.0, BudgetMoney=0.0,
         * Participant='', WeddingBookClassID=2,
         * MemberID=382, Remarks='Bbbserrt',
         * Notice='', AssistantID=174.0,
         * AssignDate=2016-08-18,
         * FinishTime=null,
         * nvc_RealName='',
         * nvc_Name='13552173733',
         * days=''}
         */
        initView();
        initData();

    }

    private void initView(){
        job_detail_photoname = (TextView) findViewById(R.id.job_detail_photoname);
        job_detail_photo = (CircularImage) findViewById(R.id.job_detail_photo);
        job_detail_remark_content_tv = (TextView) findViewById(R.id.job_detail_remark_content_tv);
        job_detail_book_place = (TextView) findViewById(R.id.job_detail_book_place);
        job_detail_missionPersonnel_name_tv = (TextView) findViewById(R.id.job_detail_missionPersonnel_name_tv);
        job_detail_Consumption_expected_money_tv = (TextView) findViewById(R.id.job_detail_Consumption_expected_money_tv);
        task_editor_tv = (TextView) findViewById(R.id.task_editor_tv);
        job_detail_isdone = (TextView) findViewById(R.id.job_detail_isdone);
        tian_shu_tv = (TextView) findViewById(R.id.tian_shu_tv);
        tian = (TextView) findViewById(R.id.tian);

    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * 给控件赋值;
     */
    private void initData(){
        //给头像添加图片
        if (!TextUtils.isEmpty(picurl)){
            x.image().bind(job_detail_photo,picurl,ImageOptionsUtil.getcircle_fixy());
        }
        //完成状态;
        if (dataBean.isIsComplete()){
            job_detail_isdone.setText("已完成");
            //显示编辑
            task_editor_tv.setVisibility(View.VISIBLE);
        }else{
            //未完成状态
            job_detail_isdone.setText("未完成");
            //判断是否有完成人
            if (dataBean.getAssistantID()!=0){
                //隐藏编辑
                task_editor_tv.setVisibility(View.INVISIBLE);
            }else{
                task_editor_tv.setVisibility(View.VISIBLE);
            }
            //一定要写在最后;
            if (flag==4){
                task_editor_tv.setVisibility(View.INVISIBLE);
            }
        }
        //显示编辑
        task_editor_tv.setOnClickListener(this);
        HEADName = dataBean.getWeddingBookID()+".jpg";
        String  money =  StrUtil.getMoneyStr(""+dataBean.getBudgetMoney());
        job_detail_Consumption_expected_money_tv.setText("¥" + money);
        if (!TextUtils.isEmpty(dataBean.getNvc_RealName())){
            job_detail_missionPersonnel_name_tv.setText(dataBean.getNvc_RealName());
        }else{
            if (!TextUtils.isEmpty(dataBean.getNvc_Name())){
                job_detail_missionPersonnel_name_tv.setText(dataBean.getNvc_Name());
            }else{
                job_detail_missionPersonnel_name_tv.setText(" ");
            }
        }
        if ("".equals(dataBean.getBookPlace())){
            job_detail_book_place.setText(" ");
        }else{
            job_detail_book_place.setText(dataBean.getBookPlace());
        }
        if (!TextUtils.isEmpty(dataBean.getRemarks())){
            job_detail_remark_content_tv.setText(dataBean.getRemarks());
        }
//        if (dataBean.getBookDate()!=null){
//            //距离完成任务的时间
//            days = DateUtils.daysBetween(refFormatNowDate(),dataBean.getBookDate());
//            tian_shu_tv.setText(days+"天");
//        }

//        if (!TextUtils.isEmpty(dataBean.getDays())){
//            tian_shu_tv.setText("距离计划完成还有"+days+"天");
//        }
        if (dataBean.getBookTitle()!=null){
            job_detail_photoname.setText(dataBean.getBookTitle());
        }
        String day = dataBean.getDays();
        if (!TextUtils.isEmpty(day)){
            if (RegularUtil.isValidDate(day)){
                tian.setText(day);
            }else{
                tian.setText("距离计划完成还有"+day+"天");
            }
        }else{
            tian.setText("距离计划完成还有--天");
        }

    }

    public void onBack(View view){
        Intent intent = new Intent();
        setResult(1,intent);
        this.finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.task_editor_tv:
                Intent intent = new Intent(this,JobDetail_Edit_Activity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(JobDetail.TASK_FLAG,dataBean);
                intent.putExtras(bundle);
                startActivityForResult(intent,1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null){
            dataBean = (MarriageProposalBean.DataBean) data.getSerializableExtra(JobDetail.TASK_FLAG);
            picurl = dataBean.getPicture();
            initData();
        }
       
        super.onActivityResult(requestCode, resultCode, data);

    }
    /**
     *   * 根据当前毫秒值转化为字符串;
     * @return
     */
    public String refFormatNowDate() {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String retStrFormatNowDate = sdFormatter.format(nowTime);
        return retStrFormatNowDate;
    }

}
